import { ref } from 'vue';
import { defineStore } from 'pinia';

export const uploadStore = defineStore('upload', () => {
	const uploadTasks = ref([]);
	
	function addUploadTask (task) {
		uploadTasks.value.push(task);
	}
	
	// 取消所有上传任务
	function cancelAllTasks () {
		uploadTasks.value.forEach((task) => {
	        task.abort(); // 调用 abort() 取消每个任务
	    });
	    
		clearTasks(); // 清空任务列表
	}
	
	// 清空任务列表
	function clearTasks() {
		uploadTasks.value = [];
	}
	
	return {
		clearTasks,
		uploadTasks,
		addUploadTask,
		cancelAllTasks
	}; 
});